package com.example.gsf.myapplication;

import android.graphics.Rect;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroupOverlay;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ListView;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity {

    private List<View> viewList = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewList = new ArrayList<>();
        Button btn1 = (Button) findViewById(R.id.btn1);
//        ListView listView = new ListView(this);
//        listView.setOnItemClickListener(null);
//        listView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return false;
//            }
//        });
//        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//            }
//
//            @Override
//            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//
//            }
//        });
        btn1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Rect rect = new Rect();
                v.getHitRect(rect);
                if(rect.contains((int)event.getRawY(), (int)event.getRawX())){
                    System.out.println(v.getHeight()+"======"+v.getX()+"======"+event.getRawX());
                    System.out.println(v.getWidth()+"========"+v.getY()+"======"+event.getRawY());
                }
                return false;
            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               System.out.println("ffffffffffffffffff");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        View decorView = getWindow().getDecorView();
        View contentView =decorView.findViewById(android.R.id.content);
        View mainRootView =((ViewGroup) contentView).getChildAt(0);
        parseViewTree(mainRootView);
    }

    public void parseViewTree(View view){

         if(view instanceof ViewGroup){
             ViewGroup viewGroup = (ViewGroup) view;
             viewList.add(viewGroup);
             for (int i = 0 ; i < viewGroup.getChildCount() ; i++){
                 parseViewTree(viewGroup.getChildAt(i));
             }
         } else {
             viewList.add(view);
         }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {


        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:

                for (int i = 0 ; i < viewList.size() ; i++){
                    try {
                        Method  method =  getViewClass(viewList.get(i).getClass()).getDeclaredMethod("getListenerInfo");
                        method.setAccessible(true);
                        Object obj = method.invoke(viewList.get(i));
                        Object listener = obj.getClass().getDeclaredField("mOnClickListener").get(obj);
                        if(listener != null){
                            final View.OnClickListener clickListener = (View.OnClickListener)listener;
                            viewList.get(i).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    System.out.println("btn filter is success");
                                    clickListener.onClick(v);
                                }
                            });
                        }

                    }  catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }

                }

                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        boolean result = super.dispatchTouchEvent(ev);

        return result;
    }

    public Class<View> getViewClass(Class cl){
        if(cl == View.class){
           return cl;
        } else {
           return getViewClass(cl.getSuperclass());
        }
    }
}
